//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

#ifndef TIMER_H_
#define TIMER_H_

#include <cstdint>
#include <functional>
#include <memory>

namespace como {

typedef std::function<void()> TimerTask;

class Timer {
public:
    Timer(uint32_t id, int64_t when_ms, int64_t interval_ms, const TimerTask& task);

    void Run();

    uint32_t id() const {
        return id_;
    }

    int64_t when_ms() const {
        return when_ms_;
    }

    bool repeated() const {
        return repeated_;
    }

    void UpdateWhenTime() {
        when_ms_ += interval_ms_;
    }

private:
    uint32_t    id_;
    TimerTask   task_;
    int64_t     when_ms_;
    uint32_t    interval_ms_;
    bool        repeated_;
};

using TimerPtr = std::shared_ptr<Timer>;

} // namespace como

#endif  // TIMER_H_
